<?php
/*
**微信jsapi支付
*/
require_once 'api.php';
$request=$_SERVER['QUERY_STRING'];
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$recent_url=dirname($http_type.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]);
$mchid = 'a0e5b19a8b4047c88184412997a421d1';
$private_key = 'a0c76773b8ca44ac9fa5100f5675c95f';
parse_str($request,$array);
try {    
    //创建订单支付编号
    $data=array(
            'mchid'     	=> $mchid,
            'out_trade_no'	=> time(),
            'total_fee' 	=> '0.01'*100,
            'body'  		=> '微信jsapi支付测试',
            'notify_url'	=> $recent_url.'/notify.php',
            'nonce_str' 	=> str_shuffle(time())
        );
		$url ='https://admin.xunhuweb.com/pay/jsapi';
        		if(!$_GET['openid']){
        			exit('openid获取失败');
        			}
        			$data['openid']=$_GET['openid'];
        			unset($data['type']);
					$data['sign']     =XH_Payment_Api::generate_xh_hash($data,$private_key);
	        		$response   	  =XH_Payment_Api::http_post_json($url, json_encode($data));
					$result     	  =$response?json_decode($response,true):null;
					$jsapi			  =json_decode($result['jsapi']);
					if($jsapi){  
						?>
						 <p style="margin: 50px 0 0 0; text-align: center;">
							<img src="" width="16" height="16" alt="" /> 微信支付加载中，请稍候...
						</p>
						<script>
					    if (typeof WeixinJSBridge == "undefined") {
					        if (document.addEventListener) {
					            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
					        } else if (document.attachEvent) {
					            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
					            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
					        }
					    }
					    function onBridgeReady() {
					        WeixinJSBridge.invoke(
					                'getBrandWCPayRequest', {
					                    // 以下参数通过上述接口返回的jsapi参数重获取
					                    // **************************
					                    "appId":	  '<?php echo $jsapi->appId ?>',
					                    "timeStamp":  '<?php echo $jsapi->timeStamp ?>',
					                    "nonceStr":   '<?php echo $jsapi->nonceStr ?>',
					                    "package":    '<?php echo $jsapi->package ?>',
					                    "signType":   '<?php echo $jsapi->signType ?>',
					                    "paySign":    '<?php echo $jsapi->paySign ?>',
					                    // **************************
					                },
					                function (res) {
					                    if (res.err_msg == "get_brand_wcpay_request:ok") {
					                    	location.href='<?php echo $recent_url.'/return.php' ?>';
					                    }
					                }
					        );
					    }
					</script>
				<?php
        		}
        		exit;
   } catch (Exception $e) {
      exit;
}
?>